<?php

namespace App\Http\Requests\Admin;

use App\Http\Models\Admin\Admin;
use App\Http\Models\App\App;
use App\Http\Requests\Common\BaseRequest as Request;

class AdminRequest extends Request
{

    //
    public function center()
    {
        $data = [
            'target' => 'app',
            'title'  => '管理中心',
            'admin'  => $this->admin,
        ];
        return View('admin.admin.center', compact('data'));
    }
    //
    public function admin()
    {
        $keys = ['rows', 'type', 'page', 'sort', 'order', 'name', 'account', 'mobile', 'email', 'status'];
        $data = $this->only($keys);
        if ($data['type'] == 'ajax') {
            $model = Admin::where(function ($query) use ($data) {
                if ($data['name']) {
                    $query->where('name', 'like', "%{$data['name']}%");
                }
                if ($data['account']) {
                    $query->where('account', 'like', "%{$data['account']}%");
                }
                if ($data['mobile']) {
                    $query->where('mobile', $data['mobile']);
                }
                if ($data['email']) {
                    $query->where('email', 'like', "%{$data['email']}%");
                }
                if ($data['status']) {
                    $status = $data['status'] == 2 ? 0 : $data['status'];
                    $query->where('status', $status);
                }
            });
            $admins = $model->orderBy($data['sort'], $data['order'])->paginate($data['rows']);
            $data   = [
                'total' => $admins->total(),
                'rows'  => $admins->getCollection(),
            ];
            return \Response::json($data);
        }
        return View('admin.admin.admin');
    }
    //
    public function admin_add()
    {
        //动态验证发送过来的数据
        $rules = [
            "name"     => "required|min:2|max:20|unique:admin",
            "account"  => "required|min:2|max:20|unique:admin",
            "mobile"   => "required|min:2|max:20|unique:admin",
            "password" => "required|min:2|max:80",
            "remarks"  => "required|min:2|max:80",
        ];
        $message = array(
            "required" => ":attribute不能为空",
            "min"      => ":attribute长度必能少于:min",
            "max"      => ":attribute长度不能大于:max",
            "unique"   => ":attribute已经存在",
        );
        $attributes = array(
            "name"    => "真实姓名",
            "account" => "登录账号",
            "mobile"  => "手机号码",
            "remarks" => "备注信息",
        );
        //
        $data  = $this->only(array_keys($rules));
        $check = \Validator::make($data, $rules, $message, $attributes);
        $error = $check->messages()->first();
        if ($error) {
            return self::_json([], 1, $error);
        }
        //数据入库
        $clean = [
            'name'          => $data['name'],
            'account'       => $data['account'],
            'mobile'        => $data['mobile'],
            'password'      => md5($data['password']),
            'remarks'       => $data['remarks'],
            'status'        => 1,
            'operator_id'   => $this->admin['id'],
            'operator_name' => $this->admin['name'],
            'created_at'    => date('Y-m-d H:i:s'),
        ];
        if (!Admin::insert($clean)) {
            return self::_json([], -1, '新增失败!');
        }
        return self::_json();
    }
    //
    public function admin_edit()
    {
        //动态验证发送过来的数据
        $rules = [
            "id"       => "required|exists:admin",
            "name"     => "required|min:2|max:20",
            "account"  => "required|min:2|max:20",
            "mobile"   => "required|min:2|max:80",
            "password" => "",
            "remarks"  => "required|min:2|max:80",
            "secret"   => "",
            "status"   => "",
        ];
        $message = array(
            "required" => ":attribute不能为空",
            "min"      => ":attribute长度必能少于:min",
            "max"      => ":attribute长度不能大于:max",
            "unique"   => ":attribute已经存在",
        );
        $attributes = array(
            "id"      => "管理员ID",
            "name"    => "真实姓名",
            "account" => "登录账号",
            "mobile"  => "手机号码",
            "remarks" => "备注信息",
        );
        //
        $data  = $this->only(array_keys($rules));
        $check = \Validator::make($data, $rules, $message, $attributes);
        $error = $check->messages()->first();
        if ($error) {
            return self::_json([], 1, $error);
        }
        //检查真实姓名+账号+手机是否重复
        $_id = Admin::where('name', $data['name'])
            ->orWhere('account', $data['account'])
            ->orWhere('mobile', $data['mobile'])
            ->pluck('id');
        if (isset($_id) && ($data['id'] != $_id)) {
            return self::_json([], 1, '真实姓名或账号或手机重复！');
        }
        //更新数据
        $clean = [
            'name'          => $data['name'],
            'account'       => $data['account'],
            'mobile'        => $data['mobile'],
            'remarks'       => $data['remarks'],
            'status'        => $data['status'],
            'operator_id'   => $this->admin['id'],
            'operator_name' => $this->admin['name'],
            'updated_at'    => date('Y-m-d H:i:s'),
        ];
        //判断是否更新密码
        $data['password'] ? $clean['password'] = md5($data['password']) : '';

        if (!Admin::where('id', $data['id'])->update($clean)) {
            return self::_json([], -1, '更新失败!');
        }
        return self::_json();
    }
}
